// ActionScript file
import Modules.NavigationDetails;
import Modules.NavigationInfo;
import Modules.PostLogin;
import Modules.Windows.CMDWindow;
import Modules.Windows.ValidationErrorWin;

import com.desidea.common.CommonClass;
import com.desidea.common.CustomFormatter;
import com.desidea.common.MenuConstants;
import com.desidea.components.grid.SuperGridEvent;
import com.desidea.components.grid.button.SuperGridButton;
import com.desidea.components.spinner.BusySpinner;
import com.desidea.utils.DirtyChecker;
import com.desidea.utils.DisplayModeSetter;
import com.desidea.webservice.PartnerService;
import com.desidea.webservice.ProcessServiceRequestResultEvent;

import flash.events.Event;
import flash.events.MouseEvent;

import mx.controls.Alert;
import mx.controls.LinkButton;
import mx.controls.TextInput;
import mx.core.Application;
import mx.events.CloseEvent;
import mx.events.ValidationResultEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.utils.StringUtil;
import mx.validators.Validator;


private var _btnEditCancel:LinkButton;
private var _btnAddContact:LinkButton;
private var _btnAddActivity:LinkButton;
private var _btnAddTask:LinkButton;
private var _btnAddOpportunity:LinkButton;

private var _key:String = "";
private var _mode:String = "view";//Constants.MODE_ADD;
private var _modeSet:Boolean = false;
private var _creationCompleted:Boolean = false;
private var acctdetails:XML;

private var _cityLoaded:Boolean = false;
private var _detailsLoaded:Boolean = false;
private var _ni:NavigationInfo = null;

private var txtOtherReseller:TextInput;

public function get mode():String{
	return _mode;
}

public function set mode(value:String):void{
	this._mode = value;
	this._modeSet = true;
}

public function get key():String{
	return _key;
}

public function set key(value:String):void{
	this._key = value;
}

public function get NavInfo():NavigationInfo{
	return _ni;
}
public function set NavInfo(ni:NavigationInfo):void{
	_ni = ni;
}


private function init():void{
	txtOtherReseller = new TextInput();
	txtOtherReseller.percentWidth = 98;
	txtOtherReseller.automationName = "OTHERRESELLER";
	
	this.txtNotes.linkTextInput.visible = false;
	this.txtNotes._RichTextEditor_VRule2.visible = false;
}

private function setScreenMode():void{
	if(!_creationCompleted) return;
	
	if(this.mode == SuperGridButton.MODE_VIEW || mode == SuperGridButton.MODE_VIEWICON){
		DisplayModeSetter.getInstance().setMode(this.frmAccountInfo,false);
		rtVBox.percentWidth = 50;
		btnSubmit.enabled = false;
		ctlCity.setMode(false);
		this.lblTitle.text = "View Company Details";
	}else if(this.mode == SuperGridButton.MODE_EDIT){		//MODE_EDIT = create new
		DisplayModeSetter.getInstance().setMode(this.frmAccountInfo,true);
		btnSubmit.enabled = true;
		_btnEditCancel.enabled = false;
		
		cmbParentAccount.enabled = true;
		cmbAccountOwner.enabled = false;
		cmbAccountOwner.selectedItem = (acctdetails.USERS.entry.(@id == CommonClass.UserID.toString())[0]) as XML;
		cmbContacts.enabled = false;
		ctlCity.setMode(true);
		this.lblTitle.text = "New Company";
	}
}

private function windowCreated():void{
	_btnEditCancel = new LinkButton();
	_btnEditCancel.label = "Edit";	
	_btnEditCancel.height = this.pnlAccountInfo.ToolBox.height - 2;
	_btnEditCancel.addEventListener(MouseEvent.CLICK, function(event:Event):void{ onClick('editdetails'); });
	this.pnlAccountInfo.ToolBox.addChild(_btnEditCancel);

	_btnAddContact = new LinkButton();
	_btnAddContact.label = "New Contact";
	_btnAddContact.height = this.pnlContacts.ToolBox.height - 2;
	_btnAddContact.addEventListener(MouseEvent.CLICK, function(event:Event):void{ onClick('newcontact'); });
	this.pnlContacts.ToolBox.addChild(_btnAddContact);

	_btnAddActivity = new LinkButton();
	_btnAddActivity.label = "New Activity";
	_btnAddActivity.height = this.pnlActivities.ToolBox.height - 2;
	_btnAddActivity.addEventListener(MouseEvent.CLICK, function(event:Event):void{ onClick('newactivity'); });
	this.pnlActivities.ToolBox.addChild(_btnAddActivity);

	_btnAddTask = new LinkButton();
	_btnAddTask.label = "New Task";
	_btnAddTask.height = this.pnlTasks.ToolBox.height - 2;
	_btnAddTask.addEventListener(MouseEvent.CLICK, function(event:Event):void{ onClick('newtask'); });
	this.pnlTasks.ToolBox.addChild(_btnAddTask);

	_btnAddOpportunity = new LinkButton();
	_btnAddOpportunity.label = "New Opportunity";
	_btnAddOpportunity.height = this.pnlOpportunities.ToolBox.height - 2;
	_btnAddOpportunity.addEventListener(MouseEvent.CLICK, function(event:Event):void{ onClick("newopportunity"); });
	this.pnlOpportunities.ToolBox.addChild(_btnAddOpportunity);


	pnlContacts.height 		= dgContacts.height;
	pnlActivities.height 	= dgActivities.height
	pnlTasks.height 		= dgTasks.height
	pnlOpportunities.height = dgOpps.height

	dgContacts.addEventListener(SuperGridEvent.CLICKED, contactsGridButtonColumnClicked);
	dgActivities.addEventListener(SuperGridEvent.CLICKED, activitiesGridButtonColumnClicked);
	dgTasks.addEventListener(SuperGridEvent.CLICKED, tasksGridButtonColumnClicked);
	dgOpps.addEventListener(SuperGridEvent.CLICKED, oppsGridButtonColumnClicked);

	this.ctlCity.addEventListener("dataLoadComplete",cityDataLoaded);

	rtVBox.visible = ( StringUtil.trim(this.key).length>0);

	this._creationCompleted = true;

	this.ctlCity.loadData(CommonClass.UserID);

	this.loadData();
	
	this.cmbParentAccount.setFocus();
	
	_btnEditCancel.enabled = CommonClass.UserLoginInfo.CanCreateChildAccount;
	_btnAddContact.enabled = CommonClass.UserLoginInfo.CanCreateContact;
	_btnAddActivity.enabled = CommonClass.UserLoginInfo.CanCreateActivity;
	_btnAddOpportunity.enabled = CommonClass.UserLoginInfo.CanCreateOpportunity;
	_btnAddTask.enabled = CommonClass.UserLoginInfo.CanCreateTask;
	
}

private function loadData():void{
	var params:String = "OPERATION=ACCOUNT_GETDETAILS" +
						"&PKID=" + escape(StringUtil.trim(this.key)) + 
						"&OWNERID=" + CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForAccountDetails);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
}

private function callForAccountDetails(event:ProcessServiceRequestResultEvent):void{
	acctdetails = XML(event.result);
	if(acctdetails.RESPONSE.RESULT == "E"){
		BusySpinner.getInstance().stop();
		Alert.show(acctdetails.RESPONSE.MESSAGE);
	}else if(acctdetails.RESPONSE.RESULT == 'S'){
		setDataOnControls(acctdetails);
		setScreenMode();
		this.loadAccountContacts();
		this.loadAccountActivities();
		this.loadAccountTasks();
		this.loadAccountOpps();
		this.callLater(initializeForDirtyCheck);
		
		this._detailsLoaded = true;
	}
}

private function partnerServiceFaultHandler(event:FaultEvent):void{
	BusySpinner.getInstance().stop();
	Alert.show(event.fault.toString());
}

private function cityDataLoaded(event:Event):void{
	this._cityLoaded = true;
	this.initializeForDirtyCheck();	
}

private function refSourceChanged():void{

	if(this.cmbReferralSource.selectedItem != null){
		var refSourceID:String = this.cmbReferralSource.selectedItem.@id;
		
		if(refSourceID=="8"){
			this.fiReseller.removeAllChildren();
			this.fiReseller.addChild(this.txtOtherReseller);
		}else{
			this.fiReseller.removeAllChildren();
			this.fiReseller.addChild(this.cmbResellerName);
			
			var lst:XMLList = acctdetails.RESELLERACCOUNTS.entry.(@refsrcid == refSourceID || @refsrcid == "0") as XMLList;
			if(lst.length() > 1 || refSourceID == "0"){
				cmbResellerName.dataProvider = lst
			}else{
				cmbResellerName.dataProvider = acctdetails.RESELLERACCOUNTS.entry as XMLList;
			}
		}
	}
}

private function setDataOnControls(response:XML):void{
	this.key = response.PKID;

	cmbParentAccount.dataProvider = new XML(response.PARENTACCOUNTS).entry as XMLList;
	cmbParentAccount.selectedItem = (response.PARENTACCOUNTS.entry.(@id == response.RELATIONS.RELATION.(@type == "PARENT_ACCOUNT"))[0]) as XML
	cmbParentAccount.selectedIndex = (cmbParentAccount.selectedIndex <0)?0:cmbParentAccount.selectedIndex;
	
	cmbAccountOwner.dataProvider = new XML(response.USERS).entry as XMLList;
	cmbAccountOwner.selectedIndex = 0;
	cmbAccountOwner.selectedItem = (response.USERS.entry.(@id == response.RELATIONS.RELATION.(@type == "OWNER"))[0]) as XML
	
	cmbContacts.dataProvider = new XML(response.CONTACTS).entry as XMLList;
	cmbContacts.selectedIndex = 0;
	cmbContacts.selectedItem = (response.CONTACTS.entry.(@id == response.RELATIONS.RELATION.(@type == "PRIMARY_CONTACT"))[0]) as XML ///check PRIMARY_CONTACT
	
	cmbAccountType.dataProvider = new XML(response.ACCOUNTTYPES).entry as XMLList;
	cmbAccountType.selectedItem = (response.ACCOUNTTYPES.entry.(@id == response.ACCOUNTTYPEID)[0]) as XML
	cmbAccountType.selectedIndex = (cmbAccountType.selectedIndex<0)?0:cmbAccountType.selectedIndex;
	
	cmbState.dataProvider = new XML(response.STATES).entry as XMLList;
	cmbState.selectedIndex = 0;
	cmbState.selectedItem = (response.STATES.entry.(@id == response.STATE)[0]) as XML

	cmbVertical.dataProvider = new XML(response.VERTICALS).entry as XMLList;
	cmbVertical.selectedIndex = 0;
	cmbVertical.selectedItem = (response.VERTICALS.entry.(@id == response.VERTICALID)[0]) as XML

	cmbReferralSource.dataProvider = new XML(response.REFSOURCES).entry as XMLList;
	cmbReferralSource.selectedIndex = 0;
	cmbReferralSource.selectedItem = (response.REFSOURCES.entry.(@id == response.REFERRALSOURCEID)[0]) as XML

	refSourceChanged();
		
//	cmbResellerName.dataProvider = new XML(response.RESELLERACCOUNTS).entry.(@) as XMLList;
//	cmbResellerName.selectedIndex = 0;
	cmbResellerName.selectedItem = (response.RESELLERACCOUNTS.entry.(@id == response.RESELLERID)[0]) as XML ///check PRIMARY_CONTACT
	
	this.ctlCity.SelectedCityNo = new Number(response.CITYNO);
	
	CommonClass.setDataOnComponents(this.txtAccountName, response);
	CommonClass.setDataOnComponents(this.txtNoOfLocations, response);
	CommonClass.setDataOnComponents(this.txtAddress, response);
	CommonClass.setDataOnComponents(this.txtZipCode, response);
	CommonClass.setDataOnComponents(this.txtTelephoneNo, response);
	CommonClass.setDataOnComponents(this.txtFaxNo, response);
	CommonClass.setDataOnComponents(this.txtWebsite, response);
	CommonClass.setDataOnComponents(this.txtNotes, response);
	CommonClass.setDataOnComponents(this.chkActive, response);
	CommonClass.setDataOnComponents(this.txtOtherReseller, response);

	txtTelephoneNo.text = CustomFormatter.getInstance().convertToPhoneString(txtTelephoneNo.text);
	txtFaxNo.text = CustomFormatter.getInstance().convertToPhoneString(txtFaxNo.text);
}

private function getPostData():String{

	if( String(this.cmbReferralSource.selectedItem.@id) == "8"){
		this.cmbResellerName.selectedIndex = 0;
	}else{
		this.txtOtherReseller.text = "";
	}

	txtTelephoneNo.text = CustomFormatter.getInstance().convertToPhoneString(txtTelephoneNo.text);
	txtFaxNo.text = CustomFormatter.getInstance().convertToPhoneString(txtFaxNo.text);

	var postData:String = new String();
	postData += "ACCOUNTNO=" + (acctdetails.ACCOUNTNO.toString())			+ "&";
	postData += "ACCOUNTGUID=" + (acctdetails.ACCOUNTGUID.toString())		+ "&";
	postData += (CommonClass.preparePostString(this.txtAccountName))			+ "&";
	postData += (CommonClass.preparePostString(this.txtNoOfLocations))			+ "&";
	postData += (CommonClass.preparePostString(this.cmbAccountType))			+ "&";
	postData += (CommonClass.preparePostString(this.txtAddress))				+ "&";

//	postData += (CommonClass.preparePostString(this.cmbCity))					+ "&";
	postData += "CITYNO=" + (ctlCity.SelectedCityNo.toString())					+ "&";
	postData += "CITYNAME=" + escape(StringUtil.trim(ctlCity.CityName))			+ "&";

	postData += (CommonClass.preparePostString(this.cmbState))					+ "&";
	postData += (CommonClass.preparePostString(this.txtZipCode))				+ "&";
	postData += (CommonClass.preparePostString(this.txtTelephoneNo))			+ "&";
	postData += (CommonClass.preparePostString(this.txtFaxNo))					+ "&";
	postData += (CommonClass.preparePostString(this.txtWebsite))				+ "&";
	postData += (CommonClass.preparePostString(this.cmbVertical))				+ "&";
	postData += (CommonClass.preparePostString(this.cmbReferralSource))			+ "&";
	postData += "NOTEID=" + (acctdetails.NOTEID.toString())						+ "&";
	postData += (CommonClass.preparePostString(this.txtNotes))					+ "&";
	postData += (CommonClass.preparePostString(this.chkActive))					+ "&";
	postData += "USERID=" + (CommonClass.UserID)								+ "&";
	postData += "ADDRESSNO=" + (acctdetails.ADDRESSNO.toString())				+ "&";
	postData += "ADDRESSGUID=" + (acctdetails.ADDRESSGUID.toString())			+ "&";
	postData += "COMMUNICATIONID=" + (acctdetails.COMMUNICATIONID.toString())	+ "&";
	postData += (CommonClass.preparePostString(this.cmbParentAccount))			+ "&";
	postData += (CommonClass.preparePostString(this.cmbAccountOwner))			+ "&";
	postData += (CommonClass.preparePostString(this.cmbContacts))				+ "&";
	postData += (CommonClass.preparePostString(this.cmbResellerName))			+ "&";
	postData += (CommonClass.preparePostString(this.txtOtherReseller));
	return postData;	
}

private function saveData(action:String, data:String=""):void{
	BusySpinner.getInstance().play(Application.application.ModuleCvs);

	var params:String = "OPERATION=ACCOUNT_UPDATEDETAILS" +
						(data.length>0?"&":"") +data;
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForUpdateAccountDetails);
	ws.processServiceRequest(params);
	
//	BusySpinner.getInstance().play(Application.application.parentApplication);
}


private function callForUpdateAccountDetails(event:ProcessServiceRequestResultEvent):void{
	
	var response:XML = XML(event.result);
//	BusySpinner.getInstance().stop();
	
	if(response.RESPONSE.RESULT == "S"){
		DirtyChecker.getInstance().resetDirtyChecker();

		if(CommonClass.NextEvent != null && CommonClass.NextEvent is Button)
			(CommonClass.NextEvent as Button).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		else {
			var ni:NavigationInfo = this.NavInfo.copy();
			ni.destinationNavObj.pkID = this.key;
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate( ni );
		}
	}else{
		BusySpinner.getInstance().stop();
		Alert.show( String(response.RESPONSE.MESSAGE) );	
	}	
	
}


private function onClick(mode:String):void{

	var srcNavObj:NavigationDetails = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_ACCOUNT, this.key);
	var destNavObj:NavigationDetails = null;
	var ni:NavigationInfo = null;
	
	switch(mode){
		case "editdetails":
			DisplayModeSetter.getInstance().setMode(this.frmAccountInfo,true);
			btnSubmit.enabled = true;
			cmbParentAccount.enabled = true;
			cmbAccountOwner.enabled = false;
			_btnEditCancel.enabled = false;
			ctlCity.setMode(true);
			this.lblTitle.text = "Edit Company Details";
			break;
		case "newcontact":
			BusySpinner.getInstance().play(Application.application.ModuleCvs);
			destNavObj = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_CONTACTS,"",SuperGridButton.MODE_EDIT);
			ni = new NavigationInfo(srcNavObj,destNavObj, this.acctdetails);
			ni.previousLink = this.NavInfo;
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
			break;		
		case "newactivity":
			BusySpinner.getInstance().play(Application.application.ModuleCvs);
			destNavObj = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_ACTIVITIES,"",SuperGridButton.MODE_EDIT);
			ni = new NavigationInfo(srcNavObj,destNavObj, this.acctdetails);
			ni.previousLink = this.NavInfo;
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
			break;		
		case "newtask":
			BusySpinner.getInstance().play(Application.application.ModuleCvs);

			destNavObj = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_TASKS,"",SuperGridButton.MODE_EDIT);
			ni = new NavigationInfo(srcNavObj,destNavObj, this.acctdetails);
			ni.previousLink = this.NavInfo;
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);

			break;		
		case "newopportunity":
			BusySpinner.getInstance().play(Application.application.ModuleCvs);
			destNavObj = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_OPPORTUNITIES,"",SuperGridButton.MODE_EDIT);
			ni = new NavigationInfo(srcNavObj,destNavObj, this.acctdetails);
			ni.previousLink = this.NavInfo;
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
			break;		
		case "openurl":
			navigateToURL( new URLRequest(this.txtWebsite.text),"_blank");
			break;
	}
}

private function buttonClick(eventType:String):void{
	switch(eventType){
		case "save":
			handleSubmitClick();
			break;
		case "cancel":
			handleCancelClick();
			break;
		default:
			Alert.show( "Yet to Implement : " + eventType);
	}
}

private function handleCancelClick():void{
	(this.parentDocument as CMDWindow).NavInfo = null;
	CommonClass.NextEvent = null;
	
	if(this.NavInfo!=null && this.NavInfo.sourceNavObj!=null){
		var src:NavigationDetails = (this.NavInfo.previousLink==null?null:this.NavInfo.previousLink.sourceNavObj.copy());//this.NavInfo.destinationNavObj.copy();
		var dst:NavigationDetails = this.NavInfo.sourceNavObj.copy();
		var ni:NavigationInfo = new NavigationInfo(src,dst);
		ni.previousLink = (this.NavInfo.previousLink == null? null:this.NavInfo.previousLink.previousLink);
		(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
	}else{
		(this.parentDocument as CMDWindow).loadModule();
	}
}

private function handleSubmitClick():void{
	if(validate()){
		saveData("", getPostData());
	}else{
		CommonClass.NextEvent = null;
	}
}

// Load data for Account Contacts Grid
private function loadAccountContacts():void{
	var params:String = "OPERATION=ACCOUNT_CONTACTS" +
						"&accountGUID=" + escape(acctdetails.ACCOUNTGUID) +
						"&USERID=" + CommonClass.UserID +
						"&ISACTIVE=" + escape(acctdetails.ISACTIVE);
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForAccountContacts);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
}

private function callForAccountContacts(event:ProcessServiceRequestResultEvent):void{
	var acctcontacts:XML = XML(event.result);
	if(acctcontacts.RESPONSE.RESULT == "E"){
		Alert.show(acctcontacts.RESPONSE.MESSAGE);
	}else if(acctcontacts.RESPONSE.RESULT == 'S'){
		dgContacts.dataProvider = acctcontacts;
		dgContacts.refreshDataProvider(0);
	}
	setContactsGridColumnWidths(dgContacts);
}

private function setContactsGridColumnWidths(grid:SuperGrid):void{
	CommonClass.setColumnWidth(grid, 1, 110);
	CommonClass.setColumnWidth(grid, 2, 75);
	CommonClass.setColumnWidth(grid, 3, 90);
}

// Load data for Account Activities Grid
private function loadAccountActivities():void{
	var params:String = "OPERATION=ACCOUNT_ACTIVITIES" +
						"&accountGUID=" + escape(acctdetails.ACCOUNTGUID) +
						"&USERID=" + CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForAccountActivities);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
}

private function callForAccountActivities(event:ProcessServiceRequestResultEvent):void{
	var acctActivities:XML = XML(event.result);
	if(acctActivities.RESPONSE.RESULT == "E"){
		Alert.show(acctActivities.RESPONSE.MESSAGE);
	}else if(acctActivities.RESPONSE.RESULT == 'S'){
		dgActivities.dataProvider = acctActivities;
		dgActivities.refreshDataProvider(0);
	}
	setActivitiesGridColumnWidths(dgActivities);
}
private function setActivitiesGridColumnWidths(grid:SuperGrid):void{
	CommonClass.setColumnWidth(grid, 1, 100);
	CommonClass.setColumnWidth(grid, 2, 90);
	CommonClass.setColumnWidth(grid, 3, 60);
	CommonClass.setColumnWidth(grid, 4, 100);
}

// Load data for Account Tasks Grid
private function loadAccountTasks():void{
	var params:String = "OPERATION=ACCOUNT_TASKS" +
						"&accountGUID=" + escape(acctdetails.ACCOUNTGUID) +
						"&USERID=" + CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForAccountTasks);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
}

private function callForAccountTasks(event:ProcessServiceRequestResultEvent):void{
	var acctTasks:XML = XML(event.result);
	if(acctTasks.RESPONSE.RESULT == "E"){
		Alert.show(acctTasks.RESPONSE.MESSAGE);
	}else if(acctTasks.RESPONSE.RESULT == 'S'){
		dgTasks.dataProvider = acctTasks;
		dgTasks.refreshDataProvider(0);
	}
	setTasksGridColumnWidths(dgTasks);
}

private function setTasksGridColumnWidths(grid:SuperGrid):void{
	CommonClass.setColumnWidth(grid, 1, 110);
	CommonClass.setColumnWidth(grid, 2, 75);
	CommonClass.setColumnWidth(grid, 3, 80);
	CommonClass.setColumnWidth(grid, 4, 50);
}



// Load data for Account Opportunities Grid
private function loadAccountOpps():void{
	var params:String = "OPERATION=ACCOUNT_OPPORTUNITIES" +
						"&accountGUID=" + escape(acctdetails.ACCOUNTGUID) +
						"&USERID=" + CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForAccountOpps);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
}

private function callForAccountOpps(event:ProcessServiceRequestResultEvent):void{
	var acctOpps:XML = XML(event.result);
	if(acctOpps.RESPONSE.RESULT == "E"){
		Alert.show(acctOpps.RESPONSE.MESSAGE);
	}else if(acctOpps.RESPONSE.RESULT == 'S'){
		dgOpps.dataProvider = acctOpps;
		dgOpps.refreshDataProvider(0);
	}
//	setOppsGridColumnWidths(dgOpps);
}

private function setOppsGridColumnWidths(grid:SuperGrid):void{
	CommonClass.setColumnWidth(grid, 1, 110);
	CommonClass.setColumnWidth(grid, 2, 80);
}


private function contactsGridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event, "contacts");
}

private function activitiesGridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event, "activities");
}

private function tasksGridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event, "tasks");
}

private function oppsGridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event, "opps");
}

private function handleGridButtonClick(event:SuperGridEvent, moduleCode:String = null):void{
	var mode:String =  String(event.data.linkId).toLowerCase();

	var srcNavObj:NavigationDetails = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_ACCOUNT, this.key);
	var destNavObj:NavigationDetails = null;
	var ni:NavigationInfo = null;
	
	switch(moduleCode){
		case "contacts":
			BusySpinner.getInstance().play(Application.application.ModuleCvs);
			destNavObj = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_CONTACTS,event.data.data["key"].toString());
			ni = new NavigationInfo(srcNavObj,destNavObj);
			ni.previousLink = this.NavInfo;
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
			break;
		case "activities":
			BusySpinner.getInstance().play(Application.application.ModuleCvs);
			destNavObj = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_ACTIVITIES,event.data.data["key"].toString());
			ni = new NavigationInfo(srcNavObj,destNavObj);
			ni.previousLink = this.NavInfo;
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
			break;
		case "tasks":
			BusySpinner.getInstance().play(Application.application.ModuleCvs);
			destNavObj = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_TASKS,event.data.data["key"].toString());
			ni = new NavigationInfo(srcNavObj,destNavObj);
			ni.previousLink = this.NavInfo;
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
			break;
		case "opps":
			BusySpinner.getInstance().play(Application.application.ModuleCvs);
			destNavObj = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_OPPORTUNITIES,event.data.data["key"].toString());
			ni = new NavigationInfo(srcNavObj,destNavObj);
			ni.previousLink = this.NavInfo;
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
			break;
	}
}

private function validate():Boolean {
	
	var v:Array = new Array();
	v.push(nameV,locationV,accTypeV);
	v.push(addressV, stateV, zipV);
	v.push(phoneV,faxV);
	v.push(verticalV,refSrcV);
	
	var result:Array = Validator.validateAll(v);

	var cityValidation:ValidationResultEvent = this.ctlCity.validate(fiCity.required);
	if(cityValidation.type == ValidationResultEvent.INVALID)
		result.push(cityValidation);

	if(result.length>0){
		var vWin:ValidationErrorWin = new ValidationErrorWin();
		vWin.Messages = result;
		vWin.title = "Save : Company Details";
		
		PopUpManager.addPopUp(vWin,this,true);
		PopUpManager.centerPopUp(vWin);
		return false;
	}
	return true;
}

private function initializeForDirtyCheck():void{
	if(!_cityLoaded || !_detailsLoaded) return;

	BusySpinner.getInstance().stop();
	DirtyChecker.getInstance().setInitialControl(this);
}
public function promptToSave():void{
	Alert.buttonWidth = 90;
	var yesNoCancelAlert: Alert = Alert.show("Do you want to save before exiting?", "Save",Alert.YES | Alert.NO | Alert.CANCEL, Application.application.ModuleCvs,performAlertAction);
}

private function performAlertAction(event:CloseEvent):void{
	if(event.detail == Alert.YES){
		handleSubmitClick();
	}else if(event.detail == Alert.NO){
		DirtyChecker.getInstance().resetDirtyChecker();
		if(CommonClass.NextEvent != null && CommonClass.NextEvent is Button)
			(CommonClass.NextEvent as Button).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		else
			handleCancelClick();
	}else{
		// PERFORM NO ACTION - STAY BACK TO THE PAGE
	}
}